package myservice.android.utilities;

import myservice.android.R;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.app.Activity;

public class PopupMenu {
	
	private static LayoutInflater inflater;
	private static PopupWindow pw;
	public static TextView menuHeading, menuOption1, menuOption2, menuOption3, menuOption4;
	public static LinearLayout menuSection2,menuSection3,menuSection4;	
	private static View popupView;	
	
	public static void createPopupMenu(Activity activity) {
		inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		popupView = inflater.inflate(R.layout.popup_menu, null, false);	
		
		pw = new PopupWindow(activity.getApplicationContext());
		pw.setTouchable(true);
		pw.setFocusable(true);
		pw.setOutsideTouchable(true);
		pw.setTouchInterceptor(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					pw.dismiss();

					return true;
				}

				return false;
			}
		});

		pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
		pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
		pw.setOutsideTouchable(false);
		pw.setContentView(popupView);
		
		menuHeading = (TextView) popupView.findViewById(R.id.menuHeading);
		menuOption1 = (TextView) popupView.findViewById(R.id.option1);		
		menuOption2 = (TextView) popupView.findViewById(R.id.option2);	
		menuOption3 = (TextView) popupView.findViewById(R.id.option3);
		menuOption4 = (TextView) popupView.findViewById(R.id.option4);
		menuSection2 = (LinearLayout) popupView.findViewById(R.id.optionLayout2);
		menuSection3 = (LinearLayout) popupView.findViewById(R.id.optionLayout3);
		menuSection4 = (LinearLayout) popupView.findViewById(R.id.optionLayout4);	
		
	}
	
	public static void showMenu(View view) {
		pw.showAtLocation(view, Gravity.CENTER, 0, 0);		
	}
	
	public static void closeMenu() {
		pw.dismiss();		
	}

}
